import setting from '../settings.js'
import store from '../store/index.js'

// 封装请求函数
function request(option = {}) {
	const header = {
		'content-type': "application/x-www-form-urlencoded"
	}
	if(store.getters.token) {
		header.Authorization = store.getters.token
	}
	return new Promise((resolve, reject) => {
		uni.request({
			url: setting.baseUrl + option.url,
			data: option.data,
			header: header,
			method: option.method,
			timeout: 30000,
			dataType: 'json',
			success: (res) => {
				if (res.data.code == 200) {
					resolve(res.data)
				} else if(res.data.code == 514 || res.data.code === 508){
					console.log('登录失效');
					store.dispatch('user/logout')
					uni.navigateTo({
						url: '/pages/login/login.vue'
					})
					reject(res.data)
				} else {
					// console.error(res.data.msg);
					reject(res.data)
				}
			},
			fail: (err) => {
				console.error(err.data.msg)
				console.log(err);
				console.log('err-------------------------');
				reject(err)
			}
		})
	})
}

export default request
